import type { PaginationResult } from "../../models";
import type {
  ConditionsItem,
  EnergySummary,
  EnergyTrendsItem,
  EquipmentSummaryItem,
  EquipmentSummaryReq,
  ExtrendData,
  MyProjectItem,
  OperationUserSearchReq,
  ProjectItem,
  ProjectMemberItem,
  ProjectRoleItem,
  ProjectUser,
  ReqProject,
  ReqProjectInfo,
  SectionItem,
  TrendsSectionItem,
} from "./models";
import req from "/@/api/req";

export enum Api {
  // 已改成新接口 aiot模块项目弃用
  projectsPage = "api/v1/enterprises/projects/page",
  addProject = "api/v1/enterprises/projects/create",
  updateProject = "api/v1/enterprises/projects/update",
  getProjectDetail = "api/v1/enterprises/projects/get",
  getProjectSummaryVo = "api/v1/enterprises/projects/getProjectSummaryVoByProjectId",
  getWorkOperatorList = "api/iopm/workOrderOperations/getWorkOperatorList",
  memberList = "api/v1/projects/settings/members/list",
  getProjectSummaryVoByProjectId = "api/aiot/projects/getProjectSummaryVoByProjectId",
  searchConditions = "api/v1/equipments/conditions",
  delMembers = "api/v1/projects/settings/members/delete",
  createMembers = "api/v1/projects/settings/members/create",
  updateMember = "api/v1/projects/settings/members/update",
  //end
  deleteProjectsByIds = "api/v1/enterprises/projects/batch_delete",
  section = "api/v1/projects/energies/sections",
  trendsSection = "api/v1/projects/energies/trends/sections",
  //项目设置
  diagrams = "api/v1/scadas/diagrams",
  queryProjectList = "api/aiot/projects/common/queryList",
  queryProjectGetById = "api/aiot/projectInfo/common/getById",
  getProjectVoByProjectId = "api/aiot/projects/getProjectVoByProjectId",
  equipmentSummary = "api/aiot/iotBoards/equipmentSummary",
  energySummary = "api/v1/projects/energies/summary",
  energyTrends = "api/v1/projects/energies/trends",
  getMyProjects = "api/aiot/projects/getMyProjectsByUserId",
  // 趋势图
  forecastChart = "api/v1/projects/energies/forecast_trends",
}

// 获取设备分组查询数据
export function searchConditions() {
  return req.post<ConditionsItem>(
    {
      url: Api.searchConditions,
      data: {},
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 获取项目成员列表
export function memberList(data: { projectId?: string; projectRoleId?: string; searchText?: string }) {
  return req.post<Array<ProjectMemberItem>>({
    url: Api.memberList,
    data,
  });
}
// 获取项目成员列表
export function getProjectSummaryVoByProjectId(data: { projectId?: string }) {
  return req.post<Array<ProjectMemberItem>>({
    url: Api.getProjectSummaryVoByProjectId,
    data,
  });
}
export function projectsPage() {
  return req.post<PaginationResult<ProjectItem>>(
    {
      url: Api.projectsPage,
      data: {
        current: 1,
        model: {},
        order: "descending",
        size: 999,
        sort: "createdOn",
      },
    },
    {
      useGlobalLoading: false,
    }
  );
}
export function addProject(data: ReqProjectInfo) {
  return req.post<ProjectItem>({
    url: Api.addProject,
    data,
  });
}
// 更新项目
export function updateProject(data: ReqProjectInfo) {
  return req.post<ProjectItem>({
    url: Api.updateProject,
    data,
  });
}

export function equipmentSummary(data: EquipmentSummaryReq, useGlobalLoading = true) {
  return req.post<EquipmentSummaryItem>(
    {
      url: Api.equipmentSummary,
      data,
    },
    {
      useGlobalLoading,
    }
  );
}
export function getProjectVoByProjectId(params: ReqProject) {
  return req.post<ProjectItem>({
    url: Api.getProjectVoByProjectId,
    data: params,
  });
}

export function getProjectDetail(data: { projectId: string }, useGlobalLoading = false) {
  return req.post<ReqProjectInfo>(
    {
      url: Api.getProjectDetail,
      data,
    },
    {
      useGlobalLoading,
    }
  );
}

export function getProjectSummaryVo(data: { projectId: string }, useGlobalLoading = false) {
  return req.post<ReqProjectInfo>(
    {
      url: Api.getProjectSummaryVo,
      data,
    },
    {
      useGlobalLoading,
    }
  );
}

// 获取派单人员列表
export function getWorkOperatorList(data: OperationUserSearchReq) {
  return req.post<Array<ProjectUser>>(
    {
      url: Api.getWorkOperatorList,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}

// 获取能耗数据
export function energySummary(data: EnergySummary) {
  return req.post<any>(
    {
      url: Api.energySummary,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 获取能耗趋势数据
export function energyTrends(data: EnergySummary) {
  return req.post<EnergyTrendsItem>(
    {
      url: Api.energyTrends,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 获取分区列表

export function getSection(data: { projectId: string }) {
  return req.post<SectionItem[]>(
    {
      url: Api.section,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 获取分区列表能耗数据
export function getTrendsSection(data: EnergySummary) {
  return req.post<TrendsSectionItem>(
    {
      url: Api.trendsSection,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}

export function delMembers(ids: string[]) {
  return req.commonPost<boolean>(Api.delMembers, ids);
}

export function createMembers(data: ProjectItem) {
  return req.commonPost<boolean>(Api.createMembers, data);
}
export function delProject(data: string[]) {
  return req.commonPost<boolean>(Api.deleteProjectsByIds, data);
}

export function updateMember(data: ProjectMemberItem) {
  return req.commonPost<boolean>(Api.updateMember, data);
}

export function getMyProjects() {
  return req.commonPost<MyProjectItem[]>(Api.getMyProjects, {}, { useGlobalLoading: false });
}
export function forecastChart(data: { period: string; projectId: string }) {
  return req.commonPost<ExtrendData>(Api.forecastChart, data, { useGlobalLoading: false });
}
